package com.sdy.kzyy.pc.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.kzyy.biz.dto.BookingSituationDTO;
import com.sdy.kzyy.biz.dto.StoreManageDTO;
import com.sdy.kzyy.biz.dto.SweepCodeStaffDTO;
import com.sdy.kzyy.biz.model.Manager;
import com.sdy.kzyy.biz.model.Store;
import com.sdy.kzyy.biz.model.User;
import com.sdy.kzyy.biz.service.ManagerService;
import com.sdy.kzyy.biz.service.StoreService;
import com.sdy.kzyy.biz.service.UserService;
import com.sdy.kzyy.pc.util.Constants;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.insert.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.kzyy.pc.controller
 * @date:2020/3/26
 */

@Slf4j
@RestController
@RequestMapping("/getSweepCodeStaff")
@Api(tags = "扫码人员管理")
public class SweepCodeStaffController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private ManagerService managerService;

    @GetMapping("/getSweepPage")
    @ApiOperation(value = "扫码人员管理分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "state", value = "授权类型 1:授权 2:未授权"),
    })
    public Response getSweepPage(HttpServletRequest request,String name, Integer state, Page<User> page) {
        if (name != null){
            try {
                name = URLDecoder.decode(name, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Manager byId = managerService.getById(userId);
        if(byId == null){
            Response.error("当前药店无人员");
        }
        String storeCode = byId.getStoreCode();
        List<SweepCodeStaffDTO> sweepPage = userService.getSweepPage(storeCode,name, state, page);
        Integer sweepSize = userService.getSweepSize(storeCode,name, state);
        IPage<SweepCodeStaffDTO> storeVoPage = new Page<SweepCodeStaffDTO>(page.getCurrent(), page.getSize(), sweepSize).setRecords(sweepPage);
        return Response.success(storeVoPage);
    }

    @GetMapping("/getSweepExcel")
    @ApiOperation(value = "扫码人员管理Excel导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "state", value = "授权类型 1:授权 2:未授权"),
    })
    public void getSweepExcel(HttpServletRequest request, HttpServletResponse response,String name, Integer state) throws IOException {
        if (name != null){
            try {
                name = URLDecoder.decode(name, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Manager byId = managerService.getById(userId);
        if(byId == null){
            Response.error("当前药店无人员");
        }
        String storeCode = byId.getStoreCode();
        List<SweepCodeStaffDTO> sweepPage = userService.getSweepPage(storeCode,name, state);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sdf.format(new Date());
        String excelName = URLDecoder.decode("扫码人员情况.xlsx", "UTF-8");
        String fileName = date + excelName;
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        EasyExcel.write(response.getOutputStream(), SweepCodeStaffDTO.class).sheet("口罩预约情况").doWrite(sweepPage);
    }

    @PostMapping("cancelPower")
    @ApiOperation(value = "取消授权", response = BookingSituationDTO.class)
    public Response cancelPower(@RequestBody @ApiParam(value = "bookingSituationDTO",name = "实体类(放入id & 状态state)") BookingSituationDTO bookingSituationDTO) throws BizException {
        Boolean aBoolean = userService.cancelPower(bookingSituationDTO);
        return Response.success(aBoolean);
    }


}
